package sap.feed.android.demo;

import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MonitorActivity extends Activity{
	
	private BusLocationDemoService service;
	private Handler handler;	
	
	private LinearLayout message_panel;
	private boolean blocked;
	
	private TextView[][] table_panel;
	
	public void onResume(){
		super.onResume();
		
        service = (BusLocationDemoService)getApplication();
        service.registerHandler(handler);		

        message_panel = (LinearLayout)findViewById(R.id.message_panel);
        
        table_panel = new TextView[2][4];
        
        table_panel[0][0] = (TextView)findViewById(R.id.bus1_id_value);
        table_panel[0][1] = (TextView)findViewById(R.id.bus1_distance_value);
        table_panel[0][2] = (TextView)findViewById(R.id.bus1_awt_value);
        table_panel[0][3] = (TextView)findViewById(R.id.bus1_seat_value);

        
        table_panel[1][0] = (TextView)findViewById(R.id.bus2_id_value);
        table_panel[1][1] = (TextView)findViewById(R.id.bus2_distance_value);
        table_panel[1][2] = (TextView)findViewById(R.id.bus2_awt_value);
        table_panel[1][3] = (TextView)findViewById(R.id.bus2_seat_value);        
        
	}
	
	public void addMessage(String header, String body){

		message_panel.removeAllViews();
		
        LinearLayout msg_layout = new LinearLayout(this);
        msg_layout.setOrientation(LinearLayout.VERTICAL);
        
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        
        TextView msg_header = new TextView(this);
        msg_header.setText(header);
        msg_header.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);

        TextView msg_body = new TextView(this);
        msg_body.setText(body);
        msg_body.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
        
        msg_layout.addView(msg_header, param);
        msg_layout.addView(msg_body, param);
        
        
        message_panel.addView(msg_layout, param);     
/*        
		Message msg = new Message();
		JSONObject msgObj = new JSONObject();
		try {
			msgObj.put("type", "Hide Message");
			msgObj.put("layout",msg_layout);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		msg.obj = msgObj;
		handler.sendMessageDelayed(msg,3000);
*/		
	}
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.monitor);
        
        service = (BusLocationDemoService)getApplication();
        service.registerHandler(handler);
        
        blocked = false;
        
        handler = new Handler(new Handler.Callback() {
			
			public boolean handleMessage(Message msg) {
				// TODO Auto-generated method stub
				JSONObject obj = (JSONObject)msg.obj;
				
				try {
					String eventType = (String)obj.get("type");
					if(eventType.equals("Hide Message")){
						message_panel.removeView((LinearLayout)obj.get("layout"));
					} else if(eventType.equals("Receive Message")){
						
						if(blocked){
							blocked = false;
							((LinearLayout)findViewById(R.id.table_panel)).setVisibility(View.VISIBLE);
						}
						
						table_panel[obj.getInt("target")][0].setText(obj.getInt("id")+"");
						table_panel[obj.getInt("target")][1].setText(obj.getInt("distance")+" bus stops");
						table_panel[obj.getInt("target")][2].setText(obj.getInt("awt")+" mins");
						table_panel[obj.getInt("target")][3].setText(obj.getString("seat"));
						
						addMessage("Receiving Messages", "from data stream: "+obj.getInt("dsID")+", published by "+obj.getInt("pubID"));
						
//						addMessage(obj.getString("title"), obj.getString("text")+"\n");
					} else if(eventType.equals("Add Message")){
						addMessage("Setting Changed", obj.getString("text"));
						if(!blocked){
							blocked = true;
							((LinearLayout)findViewById(R.id.table_panel)).setVisibility(View.INVISIBLE);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
				return true;
			}
		});        

    }

	
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if ( keyCode == KeyEvent.KEYCODE_MENU ) {
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setMessage("Are you sure you want to exit?")
    		       .setCancelable(false)
    		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		        	   service.feed.logout();
    		        	   setResult(0);
    		               MonitorActivity.this.finish();
    		           }
    		       })
    		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		                dialog.cancel();
    		           }
    		       });
    		AlertDialog alert = builder.create();
    		alert.show();
    		return true;     
    	} else if(keyCode == KeyEvent.KEYCODE_BACK){
    		setResult(1);
    	}     
    	return super.onKeyDown(keyCode, event); 
    } 	

    
}
